<template>
  <div>
    <div style="padding: 10px 0">
      <el-input style="width: 200px" placeholder="请输入用户名" v-model="username"
                suffix-icon="el-icon-search"></el-input>
      <el-button class="ml-5" type="primary" @click="loadForName">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="download">下载所有用户数据<i class="el-icon-download"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe header-cell-class-name="headerBg">
      <el-table-column prop="id" label="用户id" width="140"></el-table-column>
      <el-table-column prop="username" label="用户名" width="120"></el-table-column>
      <el-table-column prop="account" label="手机号/邮箱" width="140"></el-table-column>
      <el-table-column prop="create_time" label="注册日期" width="140"></el-table-column>
      <el-table-column prop="state" label="用户状态" width="140"></el-table-column>
      <el-table-column prop="userLog" label="用户头像"></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="success" @click="changeState(scope.row.id)"
                     v-show="scope.row.state=='normal' ? true : false">正常<i class="el-icon-circle-check"></i>
          </el-button>
          <el-button type="danger" @click="changeState(scope.row.id)" v-show="scope.row.state=='normal' ? false : true">
            禁用<i class="el-icon-circle-close"></i></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px ">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes=[10,20,30,40]
          :page-size=pageSize
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
      </el-pagination>
    </div>
  </div>
</template>

<script>

export default {
  name: "User",
  data() {
    return {
      tableData: [],
      total: 0,
      sideWidth: 200,
      username: "",
      dialogFormVisible: false,
      form: {},
      pageNum: 0,
      pageSize: 10
    }
  },

  created() {
    //请求分页查询数据
    this.load()
  },

  methods: {
    handleSizeChange(pageSize) {//设置每页展示多少条数据
      // console.log("设置每页展示多少条数据" + pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {//设置第几页
      // console.log("设置第几页" + pageNum)
      this.pageNum = pageNum
      this.load()
    },
    load() {//发起分页查询的网络请求
      this.request.get("admin/selectUser/" + this.pageNum + "/" + this.pageSize + "?username=").then(res => {
        // console.log(res)
        this.tableData = res.data.data
        this.total = res.data.total
      })
    },
    loadForName() {//发起分页查询的网络请求
      this.request.get("admin/selectUser/" + this.pageNum + "/" + this.pageSize + "?username=" + this.username).then(res => {
        // console.log(res)
        this.tableData = res.data.data
        this.total = res.data.total
      })
    },
    reset() {
      //将搜索框清空
      this.username = ""
      //刷新页面
      this.load()
    },
    changeState(id) {
      //将用户登录状态取反
      this.request.get("admin/changeState/" + id).then(res => {
        // console.log(res)
        if (res.state == 200) {
          //修改成功,刷新页面
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message);
        }
        this.load()
      })
    },
    download() {//调用系统对象，通过浏览器对象打开下面的网址下载用户数据
      // window.open('http://10.1.188.121:8160/admin/export');
      window.open('http://localhost:8160/admin/export');
      // window.open('http://adastra.isamumu.cn:8160/admin/export');
    }
  }
}
</script>

<style scoped>

</style>
